﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PES_TurnierManager
{
    /// <summary>
    /// Interaktionslogik für GroupControl.xaml
    /// </summary>
    public partial class GroupControl : UserControl
    {
        Dictionary<String, GroupTabItem> mGroupTabs;

        public delegate void NewGroupAdded();
        public delegate void MatchResultEnterdEvent(string groupName, uint matchID, uint homeScore, uint guestScore);
        public delegate void RemovePlayerFromGroupEvent(string playerName, string groupName);

        public event NewGroupAdded NewGroup;
        public event MatchResultEnterdEvent MatchResultEntered;
        public event RemovePlayerFromGroupEvent RemovePlayerFromGroup;
        
        public GroupControl()
        {
            InitializeComponent();
            mGroupTabs = new Dictionary<string, GroupTabItem>();
        }

        public void removePlayerFromGroup(string playerName, string groupName)
        {
            mGroupTabs[groupName].removePlayer(playerName);
        }

        public GroupTabItem addGroupPanel(string groupName)
        {
            GroupTabItem item = createNewGroupTab(groupName);
            TabControl.Items.Add(item);
            item.MatchResultEntered += new GroupTabItem.MatchResultEnterdEvent(item_MatchResultEntered);
            item.RemovePlayerFromTable += new GroupTabItem.RemovePlayerFromTableEvent(item_RemoveplayerFromTable);

            mGroupTabs.Add(groupName, item);

            return item;
        }

        void item_RemoveplayerFromTable(string playerName, string groupName)
        {
            if (RemovePlayerFromGroup != null)
                RemovePlayerFromGroup(playerName, groupName);
        }

        void item_MatchResultEntered(string groupName, uint matchID, uint homeScore, uint guestScore)
        {
            if (MatchResultEntered != null)
            {
                MatchResultEntered(groupName, matchID, homeScore, guestScore);
            }
        }

        /// <summary>
        /// Creates a new GroupTab with a Group Header
        /// and adds it to the mGroupTabs Member
        /// </summary>
        /// <param name="groupName">group name</param>
        /// <returns>the newly created TabItem</returns>
        private GroupTabItem createNewGroupTab(string groupName)
        {
            return new GroupTabItem(groupName);
        }

        /// <summary>
        /// Adds a player to a existing group
        /// </summary>
        /// <param name="groupName">group name</param>
        /// <param name="playerName">player name</param>
        /// <param name="teamName">name of the team used by the player</param>
        public void addPlayerToGroup(string groupName, string playerName, string teamName)
        {
            GroupTabItem groupTab = mGroupTabs[groupName];
            groupTab.addPlayer(playerName, teamName);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (NewGroup != null)
                NewGroup();
        }

        public void addMatchesToGroup(List<PES_TurnierManager.Model.Match> matches, string groupName)
        {
            mGroupTabs[groupName].addMatches(matches);
        }

        public void updateGroup(PES_TurnierManager.Model.Group group)
        {
            mGroupTabs[group.getGroupName()].updateRanking(group);
        }

    }
}